#!/usr/bin/python
# -*- coding: utf-8 -*-

#    OriginDB
#    Copyright (C) 2009  Jason Power <power.jg@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from PyQt4 import QtCore, QtGui
from ui_userAdd import Ui_UserAddWindow

import MySQLdb

import sys
sys.path.append('../')

from database.userUtils import dbHandle, login
import hashlib

def addUserToDB(username, password, email, chemdbPassword):

	hashPass = hashlib.md5(password)

	chemEncrypt = crypt(chemdbPassword, -1*int(hashPass.hexdigest(), 16))

	dbHandle.insertUser(username, hashPass.digest(), email, chemEncrypt)


def crypt (sequence, key):
	import random
	sign = (key > 0) * 2 - 1
	random.seed (abs (key * sign))
	s = ''
	for i in xrange (len (sequence)):
		r = random.randint (0, 255)
		s += chr ((ord (sequence [i]) + r * sign) % 256)
	return s


class Window(QtGui.QWidget):

	def __init__(self):
		QtGui.QWidget.__init__(self)

		self.ui = Ui_UserAddWindow()
		self.ui.setupUi(self)

		self.connect(self.ui.addPushButton, QtCore.SIGNAL("clicked()"), self.addUser)
		self.connect(self.ui.loginPushButton, QtCore.SIGNAL("clicked()"), self.login)

		self.ui.emailLineEdit.hide()
		self.ui.addPushButton.hide()
		self.ui.emailLabel.hide()

		self.ui.passwordLineEdit.setEchoMode(QtGui.QLineEdit.Password)

	def addUser(self):
		username = str(self.ui.usernameLineEdit.text())
		password = str(self.ui.passwordLineEdit.text())
		email =    str(self.ui.emailLineEdit.text())

		addUserToDB(username, password, email, "***********")

		self.dialog = QtGui.QMessageBox(QtGui.QMessageBox.Information, "User added", "User added with no problems!", QtGui.QMessageBox.Ok)
		self.dialog.show()

	def login(self):
		username = self.ui.usernameLineEdit.text()
		username = str(username)
		password = self.ui.passwordLineEdit.text()
		password = str(password)

		db = str(self.ui.dbNameLineEdit.text())
		host = str(self.ui.hostLineEdit.text())

		try:
			login(db, host)
			login(db, host, username, password)
		except ValueError or MySQLdb.OperationalError:
			self.dialog = QtGui.QMessageBox(QtGui.QMessageBox.Critical, "Error", "Did not connect to database", QtGui.QMessageBox.Ok)
			self.dialog.show()

		self.dialog = QtGui.QMessageBox(QtGui.QMessageBox.Information, "Logged in", "Logged in", QtGui.QMessageBox.Ok)
		self.dialog.show()

		self.ui.emailLineEdit.show()
		self.ui.addPushButton.show()
		self.ui.emailLabel.show()

		self.ui.loginPushButton.hide()

		self.ui.passwordLineEdit.clear()
		self.ui.usernameLineEdit.clear()
		self.ui.passwordLineEdit.setEchoMode(QtGui.QLineEdit.Normal)


def main(argv = sys.argv):
    app = QtGui.QApplication(argv)
    win = Window()
    win.show()
    return app.exec_()

if __name__ == "__main__":
    sys.exit(main())